package com.honey.feature.java;

import java.io.Reader;

import com.honey.core.builder.CharacterBuilder;
import com.honey.core.builder.CharacterBuilderFactory;
import com.honey.feature.BaseFeature;

/**
 * Java文件特征抽取. 过程是去掉文件所有没有语法意义的字符(空白字符 注释 )进行抽取, 
 * 请保证java的语法正确性,错误的语法解析会发生异常.
 * @author Administrator
 *
 */
public class JavaFeature extends BaseFeature {
	
	@Override
	public byte[] getContent(Reader reader,String charset) throws Exception {
		byte[] answer = null;
	
		final CharacterBuilder sb =CharacterBuilderFactory.createC4KStringBuilder();
		new JavaParser(reader, new Listener() {
			@Override
			public void doListener(Token token) {
				switch( token.kind ){
					case JavaParserConstants.SINGLE_LINE_COMMENT:
					case JavaParserConstants.FORMAL_COMMENT:
					case JavaParserConstants.MULTI_LINE_COMMENT:
						break;
					default:
						sb.append(token.image);
				}
			}
		}).CompilationUnit();
		
		answer = sb.toString().getBytes(charset);
		return answer;
	}
}
